package com.jiangxiaofei.citycloud.activity.village;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.VillageDetailBean;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.VillageConstant;
import com.jiangxiaofei.citycloud.mvp.VillagePersenter;
import com.jiangxiaofei.citycloud.util.BannerImageLoder;
import com.jiangxiaofei.citycloud.util.DateUtils;
import com.jiangxiaofei.citycloud.util.ImgUtils;
import com.jiangxiaofei.citycloud.view.ProgressDialogHelper;
import com.youth.banner.Banner;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by JiangXFei on 2017/8/17.
 * 美丽乡村详情页
 */

public class VillageDetailsActivity extends BaseActivity implements VillageConstant.VillageDetailView {
    @InjectView(R.id.village_detail_tv_title)
    TextView villageDetailTvTitle;
    @InjectView(R.id.village_detail_tv_date)
    TextView villageDetailTvDate;
    @InjectView(R.id.village_detail_banner_photo)
    Banner villageDetailBannerPhoto;
    @InjectView(R.id.village_detail_tv_content)
    TextView villageDetailTvContent;
    private Intent intent;
    public static final String NEWSID = "newsId";
    private int id = 0;
    private VillageConstant.VillageDetailPersenter mVillageDetailPersenter;
    private VillageDetailBean detailBean;
    public static final int ResultCode = 0X0002;

    @Override
    protected void initView(Bundle savedInstanceState) {
        mContext = this;
        mActivity = this;
        setContentView(R.layout.activity_village_detail_layout);
        ButterKnife.inject(this);
        initTitle();
        mVillageDetailPersenter = new VillagePersenter(mContext, (VillageConstant.VillageDetailView) mActivity);
        intent = getIntent();
        initDate();
    }

    @Override
    protected void setListener() {
        mRightThree.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setCTitle("美丽乡村");
        setLeftOneBg(R.drawable.ic_back);
        setRightThreeBg(R.drawable.ic_update);
    }

    private void initDate() {
        id = intent.getIntExtra(NEWSID, 0);
        if (id != 0) {
            mVillageDetailPersenter.getVillageDetail();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_rightthree:
                //菜单按钮的点击事件
                Intent intent = new Intent(mContext, VillageInfoPublishActivity.class);
                intent.putExtra(StringConstants.PUBLISHTYPE, VillageInfoPublishActivity.UPDATE);
                intent.putExtra(StringConstants.UPDATEBEAN, detailBean);
                this.startActivityForResult(intent,ResultCode );
                break;
            default:
                break;
        }
    }

    @Override
    public void setData(Object o) {
        List list = (List) o;
        if (list.get(0) instanceof VillageDetailBean) {
            detailBean = (VillageDetailBean) list.get(0);
            villageDetailTvTitle.setText(detailBean.getTitle());
            List<String> imgs = ImgUtils.getImgs(detailBean.getImg());
            villageDetailBannerPhoto.setImageLoader(new BannerImageLoder());
            villageDetailBannerPhoto.setImages(imgs);
            villageDetailBannerPhoto.start();
            villageDetailBannerPhoto.isAutoPlay(false);
            villageDetailTvContent.setText(Html.fromHtml(detailBean.getContent()));
            String date = DateUtils.unixTimestampToDate(Long.valueOf(detailBean.getAddtime()) * 1000);
            villageDetailTvDate.setText(date);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(resultCode, resultCode, data);
        if (resultCode==VillageInfoPublishActivity.resultCode){
            mVillageDetailPersenter.getVillageDetail();
        }
    }

    @Override
    public void showLoading() {
        ProgressDialogHelper.show(mContext);
    }

    @Override
    public void hideLoading() {
        ProgressDialogHelper.dismissDialog();
    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getVillageDetail() {
        return id;
    }
}
